package com.android.infantschool.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.infantschool.R;
import com.android.infantschool.bean.ClockingLeaveBean;
import com.android.infantschool.ui.imageloader.GlideRoundTransform;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @particulars
 * @time 2019\6\19 0019 8:59
 * @class describe
 */
public class ClockingLeaveAdapter extends RecyclerView.Adapter<ClockingLeaveAdapter.MyHolder> {

    private Context mContext;
    public List<ClockingLeaveBean.DataBean> list = null;
    private OnLister lister;

    public ClockingLeaveAdapter(Context context, OnLister lister) {
        this.mContext = context;
        this.list = new ArrayList<>();
        this.lister = lister;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_chocking_leave, viewGroup, false);
        MyHolder holder = new MyHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyHolder viewHolder, final int position) {
        ClockingLeaveBean.DataBean bean = list.get(position);
        Glide.with(mContext)
                .load(bean.getDescription())
                .transform(new CenterCrop(mContext), new GlideRoundTransform(mContext,10))
                .override(150,150)
                .into(viewHolder.imgLeavePhoto);
        if (bean.getLeave_type() == 1) {//事假
            viewHolder.tvLeaveType.setText("事假");
        } else if (bean.getLeave_type() == 2) {//病假
            viewHolder.tvLeaveType.setText("病假");
        }

        viewHolder.tvLeaveTime.setText(bean.getAdd_time() + "\t" + bean.getName());
        viewHolder.tvLeaveReason.setText(bean.getCause());

        if (bean.getIs_pass() == 1) {//通过
            viewHolder.tvLeaveApprovalStatus.setText("审批通过");
        } else {
            viewHolder.tvLeaveApprovalStatus.setText("待审批");
        }

        viewHolder.tvLeaveApprovalStatus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (lister != null) {
                    lister.lister(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    /**
     * 刷新数据
     *
     * @param list
     */
    public void setData(List<ClockingLeaveBean.DataBean> list) {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.list.addAll(list);
        this.notifyDataSetChanged();
    }

    /**
     * 添加数据
     *
     * @param list
     */
    public void addData(List<ClockingLeaveBean.DataBean> list) {
        this.list.addAll(0, list);
        this.notifyDataSetChanged();
    }

    /**
     * 清除数据
     */
    public void clear() {
        if (this.list != null) {
            if (this.list.size() > 0) {
                this.list.clear();
            }
        }
        this.notifyDataSetChanged();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        public ImageView imgLeavePhoto;
        public TextView tvLeaveType;
        public TextView tvLeaveTime;
        public TextView tvLeaveReason;
        public TextView tvLeaveApprovalStatus;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            imgLeavePhoto = itemView.findViewById(R.id.img_leave_photo);
            tvLeaveType = itemView.findViewById(R.id.tv_leave_type);
            tvLeaveTime = itemView.findViewById(R.id.tv_leave_time);
            tvLeaveReason = itemView.findViewById(R.id.tv_leave_reason);
            tvLeaveApprovalStatus = itemView.findViewById(R.id.tv_leave_approval_status);
        }
    }

    public interface OnLister {
        void lister(int position);
    }
}